/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Canadian Weather.
 *
 * The Initial Developer of the Original Code is
 *   George Bradt.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   George Bradt.
 *
 * Credits:
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

var textToXml = require("textToXml")
var parseXml = require('parseXml');
var data = require('self').data;
var testRunner;
var remainingTests;
var xmlObject;
var parsedData;
var degreeSymbol = "\u00B0";
var options = {
        language: 'e',  // e=English, f=French
        toolbarDisplay: {showTemperature: true,
                         showWindChill: true,
                         showHumidex: true,
                         showCondition: true,
                         showConditionIcon: true,
                         showConditionIconTooltip: true},
        toolbarDisplayEnabled: true,
        conditionsPanelEnabled: true,
        warningPanelEnabled: true
	};


// Parse the xml object.
function parse(test) {
    parsedData = parseXml.parse(xmlObject, options);
    test.pass('XML object parsed successfully');
}

// A typical site data file.
exports.testParseXmlComplete = function(test) {
    xmlObject = new textToXml.convert(data.load('xmlTest/siteDataComplete.txt'));
    parse(test);
}

exports.testBarDisplayString = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.barDisplayString, '15' + degreeSymbol + 'C Cloudy');
}

exports.testLocationName = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.location.name, 'Windsor');
}

exports.testLocationRegion = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.location.region, 'Windsor - Leamington - Essex County');
}

exports.testLocationStation = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.location.station, 'Windsor Airport');
}

exports.testCurrentConditionsDate = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.date, '10:33 PM EDT on May 25');
}

exports.testCurrentConditionsCondition = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.condition, 'Cloudy');
}

exports.testCurrentConditionsTemperature = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.temperature, '15.4' + degreeSymbol + 'C');
}

exports.testCurrentConditionsDewpoint = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.dewpoint, '14.3' + degreeSymbol + 'C');
}

exports.testCurrentConditionsWindChill = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.windChill, 'N/A');
}

exports.testCurrentConditionsHumidex = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.humidex, 'N/A');
}

exports.testCurrentConditionsPressure = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.pressure, '100.5 kPa falling');
}

exports.testCurrentConditionsVisibility = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.visibility, '24.1 km');
}

exports.testCurrentConditionsRelativeHumidity = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.relativeHumidity, '93 %');
}

exports.testCurrentConditionsWind = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.wind, 'SW 13 km/h');
}

exports.testForecastDate = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.forecasts.date, '10:31 PM EDT on May 25');
}

exports.testForecastPeriod = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.forecasts.forecast[0][0], 'Wednesday night');
}

exports.testForecastSummary = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.forecasts.forecast[0][1],
                           'Showers at times heavy with thunderstorms. Amount 20 to 30 mm. Temperature steady near 17.');
}

// A partial site data file to test calm wind.
exports.testParseXmlNoWind = function(test) {
    xmlObject = new textToXml.convert(data.load('xmlTest/siteDataNoWind.txt'));
    parse(test);
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.wind, 'Calm');
}

// A partial site data file to test wind gusts.
exports.testParseXmlWindGusts = function(test) {
    xmlObject = new textToXml.convert(data.load('xmlTest/siteDataWindGusts.txt'));
    parse(test);
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.wind, 'E 22 gust 32 km/h');
}

// A partial site data file to test wind chill.
exports.testParseXmlWindChill = function(test) {
    xmlObject = new textToXml.convert(data.load('xmlTest/siteDataWindChill.txt'));
    parse(test);
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.windChill, '-8' + degreeSymbol);
}

// A partial site data file to test optional humidex.
exports.testParseXmlHumidex = function(test) {
    xmlObject = new textToXml.convert(data.load('xmlTest/siteDataHumidex.txt'));
    parse(test);
    testRunner = test;
    testRunner.assertEqual(parsedData.currentConditions.humidex, '35' + degreeSymbol);
}

// A partial site data file to test warnings.
exports.testParseXmlWarnings = function(test) {
    xmlObject = new textToXml.convert(data.load('xmlTest/siteDataWarnings.txt'));
    parse(test);
}

// Test warnings URL.
exports.testParseXmlWarningsUrl = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.warnings[0], 'http://weatheroffice.ec.gc.ca/warnings/report_e.html?qc66');
}

// Test warnings type. Use first event.
exports.testParseXmlWarningsType = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.warnings[1].type, 'Watch');
}

// Test warnings priority. Use first event.
exports.testParseXmlWarningsPriority = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.warnings[1].priority, 'Low');
}

// Test warnings description. Use first event.
exports.testParseXmlWarningsDescription = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.warnings[1].description, 'SEVERE THUNDERSTORM WATCH  IN EFFECT');
}

// Test warnings date. Use first event.
exports.testParseXmlWarningsDate = function(test) {
    testRunner = test;
    testRunner.assertEqual(parsedData.warnings[1].date, '3:25 PM EDT on May 26');
}
